package com.leukim.ssk.client;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class Settings extends Activity {
	
	SharedPreferences mPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		mPreferences = getSharedPreferences("com.leukim.ssk.client", MODE_PRIVATE);
		
		String currentName = mPreferences.getString("username", null);
		if (currentName != null) {
			TextView tv_username = (TextView) findViewById(R.id.tv_settings_username);
			tv_username.setText(currentName);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.settings, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings_save:
			TextView tv_username = (TextView) findViewById(R.id.tv_settings_username);
			SharedPreferences.Editor editor = mPreferences.edit();
			editor.putString("username", tv_username.getText().toString());
			editor.commit();
			
			setResult(RESULT_OK);
			finish();
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		setResult(RESULT_CANCELED);
		finish();
	}

}
